<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/@tailwindcss/browser@4"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <title>Font Dot Matrix Demo</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f4f9;
            padding: 2rem;
        }

        .container {
            background-color: #fff;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        h1 {
            font-size: 1.875rem;
            font-weight: 600;
            margin-bottom: 1rem;
        }

        textarea {
            width: 100%;
            height: 200px;
            padding: 1rem;
            border: 1px solid #e2e8f0;
            border-radius: 4px;
            resize: none;
            margin-bottom: 1rem;
            font-family: 'Courier New', Courier, monospace;
            line-height: 0.5;
        }

        button {
            background-color: #4299e1;
            color: #fff;
            padding: 0.75rem 1.5rem;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #3182ce;
        }

        canvas {
            border: 1px solid #e2e8f0;
            margin-top: 1rem;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>Font Dot Matrix Parser</h1>
        <label for="inputChar">输入字符:</label>
        <input type="text" id="inputChar" placeholder="输入一个字符" value="刘" class="border border-gray-300 p-2 rounded mb-2">
        <br>
        <label for="fontFamily">字体:</label>
        <input type="text" id="fontFamily" placeholder="输入字体名称" value="宋体" class="border border-gray-300 p-2 rounded mb-2">
        <br>
        <label for="fontSize">字号:</label>
        <input type="number" id="fontSize" placeholder="输入字号" value="16" class="border border-gray-300 p-2 rounded mb-2">
        <br>
        <button id="parseButton">解析点阵数据</button>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <h3>点阵可视化</h3>
                <textarea id="output" readonly></textarea>
            </div>
            <div>
                <h3>二维数组数据</h3>
                <textarea id="arrayOutput" readonly></textarea>
            </div>
        </div>
        <canvas id="charCanvas"></canvas>
    </div>

    <script>
        function parseFontDotMatrix(fontFamily, fontSize, char) {
            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');

            const width = 16;
            const height = 16;

            canvas.width = width;
            canvas.height = height;

            ctx.clearRect(0, 0, width, height);
            ctx.font = `${fontSize}px ${fontFamily}`;
            ctx.fillStyle = 'black';

            const textMetrics = ctx.measureText(char);
            const x = (width - textMetrics.width) / 2;
            const y = textMetrics.actualBoundingBoxAscent;
            ctx.fillText(char, x, y);

            const imageData = ctx.getImageData(0, 0, width, height);
            const data = imageData.data;

            const dotMatrix = [];
            const blackDotPixelValues = [];

            for (let y = 0; y < height; y++) {
                const row = [];
                for (let x = 0; x < width; x++) {
                    const index = (y * width + x) * 4;
                    const isBlack = data[index + 3] > 128;
                    row.push(isBlack? 1 : 0);
                    if (isBlack) {
                        const r = data[index];
                        const g = data[index + 1];
                        const b = data[index + 2];
                        const a = data[index + 3];
                        blackDotPixelValues.push({ x, y, r, g, b, a });
                    }
                }
                dotMatrix.push(row);
            }

            console.log('黑色点的像素值: ', blackDotPixelValues);

            const charCanvas = document.getElementById('charCanvas');
            charCanvas.width = width;
            charCanvas.height = height;
            const charCtx = charCanvas.getContext('2d');
            charCtx.drawImage(canvas, 0, 0);

            return dotMatrix;
        }

        document.getElementById('parseButton').addEventListener('click', function () {
            const inputChar = document.getElementById('inputChar').value;
            const fontFamily = document.getElementById('fontFamily').value;
            const fontSize = parseInt(document.getElementById('fontSize').value);

            const dotMatrix = parseFontDotMatrix(fontFamily, fontSize, inputChar);
            
            // 生成点阵可视化文本
            let outputText = '';
            dotMatrix.forEach(row => {
                const rowStr = row.map(bit => bit === 1? '*' : ' ').join('');
                outputText += rowStr + '\n';
            });
            document.getElementById('output').value = outputText;

            // 生成二维数组文本
            let arrayText = '[';
            dotMatrix.forEach((row, rowIndex) => {
                arrayText += '[' + row.join(',') + ']';
                if (rowIndex < dotMatrix.length - 1) {
                    arrayText += ',\n';
                }
            });
            arrayText += ']';
            document.getElementById('arrayOutput').value = arrayText;
        });
    </script>
</body>

</html>    